package com.jluzh.exception;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
 * @author EvanHuang
 * @date 11/6/2018 9:14 AM
 * @since
 */
@Path("exception")
public class ExceptionResource {
    @POST
    @Path("register")
    public Response register(@FormParam("name")String username) {
        if ("admin".equals(username)) {
            throw new WebApplicationException("用户名已经存在!",
                    Response.Status.CONFLICT);
        } else {
            return Response.ok("注册成功!", MediaType.TEXT_PLAIN).build();
        }
    }

    @GET
    @Path("resource")
    @Produces(MediaType.APPLICATION_JSON)
    public AjaxResult doSomething(@HeaderParam("token") String token) {
        if ("token".equals(token)) {
            return new AjaxResult(true, "正常访问资源", "some logic value", 0);
        } else {
            throw new PermissionException("没有权限访问该资源", null);
        }
    }
}
